package com.ysm.freestyle.view.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.LinearLayout
import com.ysm.freestyle.R
import com.ysm.freestyle.databinding.DialogBottomTransferBinding
import com.ysm.freestyle.utils.ViewUtils
import com.ysm.freestyle.view.item.TransferItemView


//java 中继承自定义View需要实现3个构造函数，这里只实现了一个
class BottomTransferDialog(context: Context) : Dialog(context) {
    private var binding =
        DialogBottomTransferBinding.inflate(LayoutInflater.from(context))

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        setCanceledOnTouchOutside(true)
        // 把 DecorView 的默认 padding 取消，同时 DecorView 的默认大小也会取消
        window!!.decorView.setPadding(0, 0, 0, 0)
        val layoutParams = window!!.attributes
        // 设置宽度
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT
        window!!.setAttributes(layoutParams)
        // 给 DecorView 设置背景颜色，很重要，不然导致 Dialog 内容显示不全，有一部分内容会充当 padding，上面例子有举出
        window!!.decorView.setBackgroundColor(Color.TRANSPARENT)
        window!!.attributes.gravity = Gravity.BOTTOM

    }

    fun setDialogTitle(title:String): BottomTransferDialog{
        binding.tvDialogTitle.text = title
        return this
    }

    fun setCloseOnClick(onClick: () -> Unit):BottomTransferDialog {
        binding.ivBack.setOnClickListener {
            onClick()
        }
        return this
    }

    fun addItemAndOnClick(view: View, onClick: () -> Unit): BottomTransferDialog{
        val layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)
        layoutParams.topMargin = ViewUtils.dp2px(context,21f).toInt()
        val index = binding.container.childCount
        Log.i("Main","layoutParams - > $index  view width:${view.width} height:${view.height}")
        binding.container.addView(view,index,layoutParams)
        view.setOnClickListener {
            Log.i("Main","layoutParams - > onclick ")
            onClick()
        }
        return this
    }

}